﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;

public partial class Register : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        tb_name.Attributes.Add("autocomplete", "off");
        tb_sec_name.Attributes.Add("autocomplete", "off");
        tb_email.Attributes.Add("autocomplete", "off");
        tb_login.Attributes.Add("autocomplete", "off");
        tb_passwrd.Attributes.Add("autocomplete", "off");

        conn.Open();
        SqlCommand selCmd = new SqlCommand("SELECT type_licence FROM licence_type", conn);
        SqlDataReader rd = selCmd.ExecuteReader();

        while (rd.Read())
        {
            drop_licence.Items.Add(rd["type_licence"].ToString());
        }
        conn.Close();
    }

    public string MD5String(string input)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] inputBytes = Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("x2"));
        }
        return sb.ToString();
    }

    protected void bt_register_Click(object sender, EventArgs e)
    {
        int id_lic = 0;
        conn.Open();
        SqlCommand selCmd = new SqlCommand("SELECT id FROM licence_type WHERE type_licence=@type_licence", conn);
        selCmd.Parameters.Add(new SqlParameter("@type_licence", System.Data.SqlDbType.NVarChar)).Value = drop_licence.SelectedValue;
        SqlDataReader rd = selCmd.ExecuteReader();
        while (rd.Read())
        {
            id_lic = Convert.ToInt32(rd["id"].ToString());
        }
        conn.Close();

        string name = tb_name.Text;
        string sec_name = tb_sec_name.Text;
        string email = tb_email.Text;
        string id_licence = id_lic.ToString();
        string login = tb_login.Text;
        string password = tb_passwrd.Text;

        conn.Open();
        SqlCommand selCmd_login = new SqlCommand("SELECT login FROM users WHERE login=@login", conn);
        selCmd_login.Parameters.Add(new SqlParameter("@login", System.Data.SqlDbType.NVarChar)).Value = login;
        SqlDataReader rd1 = selCmd_login.ExecuteReader();
        if (rd1.HasRows)
        {
            Message.Text = "Fail: Login already exists!";
            Message.Visible = true;
        }
        else
        {
            _User.Register(name, sec_name, login, MD5String(password), email, id_licence);
            Message.Visible = true;
        }
        conn.Close();
    }
}